﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ArasPLMExtend
{
    public partial class FrmMain : Form
    {
        public FrmMain()
        {
            FrmLogin myLogin = new FrmLogin();
            if (myLogin.ShowDialog() == DialogResult.OK)
            {
                InitializeComponent();
            }
            else
            {
                System.Environment.Exit(0);
            }
            
            
        }
        private int btnflag=0;
        private void FrmMain_Load(object sender, EventArgs e)
        {
            tsslLoginDataBase.Text = ArasConnection.PublicState.PLMDatabase;
            tsslLoginUser.Text = ArasConnection.AConnection.getLoginUserName();
        }

        private void FrmMain_FormClosed(object sender, FormClosedEventArgs e)
        {
            System.Environment.Exit(0);
        }

        private void tsbQuit_Click(object sender, EventArgs e)
        {
            System.Environment.Exit(0);
        }

        private void tsbDrawFiles_Click(object sender, EventArgs e)
        {
            if(fbdFolder.ShowDialog()==DialogResult.OK)
            {
                btnflag = 0;
                CommonFunction.getFileList(fbdFolder.SelectedPath,lvwFiles);
                btnUpLoad.Enabled = true;
            }
        }

        private void tsbTZFiles_Click(object sender, EventArgs e)
        {
            FrmTZSelected mytz = new FrmTZSelected(1);
            mytz.Text = "工艺批量上传";
            if(mytz.ShowDialog()==DialogResult.OK)
            {
                btnflag = 1;
                showInfo();
                CommonFunction.getFileList(CPublicState.tzFolderPath, lvwFiles);
                btnUpLoad.Enabled = true;
            }
        }
        private void showInfo()
        {
            //显示货品信息
            lbxPrdInfo.Visible = true;
            lbxPrdInfo.Items.Clear();
            string prdno,prdname,prdspc;
            CPublicState.prdinfo.TryGetValue("prdno",out  prdno);
            CPublicState.prdinfo.TryGetValue("prdname", out  prdname);
            CPublicState.prdinfo.TryGetValue("prdspc", out  prdspc);
            lbxPrdInfo.Items.Add(prdno);
            lbxPrdInfo.Items.Add(prdname);
            lbxPrdInfo.Items.Add(prdspc);

            //显示制程信息
            lbxZCList.Visible = true;
            lbxZCList.Items.Clear();
            foreach(var s in CPublicState.tzlist)
            {
                lbxZCList.Items.Add(s.Value.zcno);
            }
        }
        private void tsbCheckFiles_Click(object sender, EventArgs e)
        {
            FrmTZSelected mytz = new FrmTZSelected(2);
            mytz.Text = "检验指导书批量上传";
            if (mytz.ShowDialog() == DialogResult.OK)
            {
                btnflag = 2;
                showInfo();
                CommonFunction.getFileList(CPublicState.checkFolderPath, lvwFiles);
            }
        }

        private void tsbImportBOM_Click(object sender, EventArgs e)
        {
            FrmBOMFromERP mybomERP = new FrmBOMFromERP();
            if(mybomERP.ShowDialog()==DialogResult.OK)
            {
                MessageBox.Show("货品及相关BOM导入成功！");
            }
            else
            {
                MessageBox.Show("货品及相关BOM导入失败！");
            }
        }

        private void btnUpLoad_Click(object sender, EventArgs e)
        {
            lbxLog.Items.Clear();
            lbxLog.Items.Add("-------------开始上传----------------");
            pgpUpload.Maximum = lvwFiles.Items.Count;
            pgpUpload.Minimum = 0;
            pgpUpload.Value = 0;
            DateTime myStartTime = DateTime.Now;
            foreach(ListViewItem s in lvwFiles.Items)
            {
                bool isOK = false;
                if (s.Text.EndsWith(".pdf"))
                {
                    string prdno = s.Text.Substring(0, s.Text.Length - 4);
                    switch (btnflag)
                    {
                        case 0:
                            if (ArasConnection.ArasFunction.checkPartByPrdNO(prdno) != "")
                            {
                                isOK = ArasConnection.CUpLoadFile.uploadFile(prdno, s.Text,
                                fbdFolder.SelectedPath);
                                if (isOK)
                                {
                                    lbxLog.Items.Add(s.Text + "---导入成功");
                                }
                                else
                                {
                                    lbxLog.Items.Add(s.Text + "---导入失败");
                                }
                            }
                            else
                            {
                                lbxLog.Items.Add(s.Text + "---导入失败");
                            }
                            break;
                        case 1:
                            if (lbxZCList.Items.Contains(prdno))
                            {
                                Common.CTFZC mytfzc=new Common.CTFZC();
                                if (CPublicState.tzlist.TryGetValue(prdno, out mytfzc))
                                {
                                    if (ArasConnection.CUpLoadFile.uploadFile(prdno, s.Text, CPublicState.tzFolderPath, "Part Process Pdf", "Part ProcessPDF", mytfzc))
                                {
                                    lbxLog.Items.Add(s.Text + "---导入成功");
                                }
                                else
                                {
                                    lbxLog.Items.Add(s.Text + "---导入失败");
                                }
                            }
                                }
                            else
                            {
                                lbxLog.Items.Add(s.Text + "---导入失败");
                            }
                                
                            break;
                        case 2:
                            if (lbxZCList.Items.Contains(prdno))
                            {
                                Common.CTFZC mytfzc = new Common.CTFZC();
                                if (CPublicState.tzlist.TryGetValue(prdno, out mytfzc))
                                {
                                    if (ArasConnection.CUpLoadFile.uploadFile(prdno, s.Text, CPublicState.tzFolderPath, "Part PCheck PDF", "Part PCheckPDF", mytfzc))
                                    {
                                        lbxLog.Items.Add(s.Text + "---导入成功");
                                    }
                                    else
                                    {
                                        lbxLog.Items.Add(s.Text + "---导入失败");
                                    }
                                }
                            }
                            else
                            {
                                lbxLog.Items.Add(s.Text + "---导入失败");
                            }
                            break;
                    }
                }
                else
                {
                    lbxLog.Items.Add(s.Text + "---导入失败,非pdf文件!");
                }
                pgpUpload.Value = pgpUpload.Value + 1;
            }
            TimeSpan st=DateTime.Now-myStartTime;
            lbxLog.Items.Add("上传完成，耗时：" + st.Seconds.ToString()+"秒");
            lbxLog.Items.Add("-------------上传完成----------------");
            btnUpLoad.Enabled = false;
            lbxPrdInfo.Visible = false;
            lbxZCList.Visible = false;
        }
    }
}
